import { EntityState, EntityStore, PaginatorPlugin, QueryEntity, StoreConfig } from '@datorama/akita';
import { produce } from 'immer';
import { VISIBILITY_FILTER } from "@/models/commonType";
import { PendingMessage } from '@/models/Message';

export interface MessageEntity extends EntityState<PendingMessage, number> {
  filter: VISIBILITY_FILTER;
}

@StoreConfig({ name: "messages", producerFn: produce, idKey: 'id' })
export class MessagesStore extends EntityStore<MessageEntity> {
  constructor() {
    super({
      filter: VISIBILITY_FILTER.SHOW_ALL,
    });
  }
}

export class MessagesQuery extends QueryEntity<MessageEntity> {
  filter$ = this.select((state) => state.filter);

  constructor(protected store: MessagesStore) {
    super(store);
  }
}

export const makeStore = (): [MessagesStore, MessagesQuery, PaginatorPlugin<MessageEntity>] => {
  const store = new MessagesStore();
  const query = new MessagesQuery(store);

  const paginator = new PaginatorPlugin(query)
    .withControls()
    .withRange();

  return [store, query, paginator];
};
